/*
 * Copyright 2011 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy
{
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.containers.VBox;
	import mx.controls.Tree;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.ListEvent;
	
	import sweezy.anim.PositionControl;
	import sweezy.anim.PropertyAnimation;
	import sweezy.easing.CheatSheet;
	import sweezy.easing.CompoundEasing;
	import sweezy.easing.PhysicalEasing;

	public class MainViewBase extends VBox
	{

		public var contents:Container;

		[Bindable]
		public var menu:XML = <root>
				<item label="ホーム" type={getQualifiedClassName(Home)}/>
				<items label="イージング">
					<item label="チートシート" type={getQualifiedClassName(CheatSheet)}/>
					<item label="合成イージング" type={getQualifiedClassName(CompoundEasing)}/>
					<item label="物理イージング" type={getQualifiedClassName(PhysicalEasing)}/>
				</items>
				<items label="アニメーション">
					<item label="プロパティアニメーション" type={getQualifiedClassName(PropertyAnimation)}/>
					<item label="再生ヘッド制御" type={getQualifiedClassName(PositionControl)}/>
				</items>
			</root>;

		public var tree:Tree;

		[Bindable]
		public var year:int = new Date().fullYear;

		private var _current:String;

		public function MainViewBase()
		{
		}

		override protected function createChildren():void
		{
			super.createChildren();

			tree.addEventListener(ListEvent.ITEM_CLICK, itemClickHandler);
		}

		private function itemClickHandler(e:ListEvent):void
		{
			var item:XML = XML(e.itemRenderer.data);
			if (item.children().length() === 0)
			{
				var name:String = String(item.@type);
				if (name === "" || name === _current)
				{
					return;
				}

				try
				{
					var type:Class = getDefinitionByName(name) as Class;
					var ui:UIComponent = new type();
					ui.percentWidth = 100;
					ui.percentHeight = 100;

					contents.removeAllChildren();
					contents.addChild(ui);
					_current = name;
				}
				catch (e:*)
				{
				}
			}
			else
			{
				tree.expandItem(item, !tree.isItemOpen(item), true);
			}
		}
	}
}
